package com.nettest.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {

    public static Date getCurrentDate() {
        Calendar calendar = new GregorianCalendar();
        return calendar.getTime();
    }

    public static int getCurrentYear() {
        Calendar calendar = new GregorianCalendar();
        return calendar.get(Calendar.YEAR);
    }
    
    public static int getCurrentMonth() {
        Calendar calendar = new GregorianCalendar();
        return calendar.get(Calendar.MONTH)+1;
    }
    
    public static boolean isCurrentMonthDate(Date date) {
    	Calendar calendar = new GregorianCalendar();
    	calendar.setTime(date);
   	    int year = calendar.get(Calendar.YEAR);
   	    int month = calendar.get(Calendar.MONTH)+1;
   	    if (year == getCurrentYear() && month == getCurrentMonth()) {
   	    	return true;
   	    } else  {
   	    	return false;
   	    }
    	
    }
    
    public static Date[] transformToDateRange(Integer year, Integer month) {
    	Calendar calendar = new GregorianCalendar();
   	    calendar.set(Calendar.YEAR, year);
   	    calendar.set(Calendar.MONTH, month-1);
     	
    	Date[] dateRange = new Date[2];
   
    	calendar.set(Calendar.DAY_OF_MONTH, 1);
    	calendar.set(Calendar.HOUR_OF_DAY, 0);
    	calendar.set(Calendar.MINUTE, 0);
    	calendar.set(Calendar.SECOND, 0);
    	dateRange[0] = calendar.getTime();
    	
    	calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    	calendar.set(Calendar.HOUR_OF_DAY, 23);
    	calendar.set(Calendar.MINUTE, 59);
    	calendar.set(Calendar.SECOND, 59);
    	dateRange[1] = calendar.getTime();
    	
    	return dateRange;
    }
    
    public static String cutoffSeconds(String time) {
    	//HH:MM:SS => HH:MM
    	if (time.length() > 5) {
    	    return time.substring(0, 5);
    	} else {
    		return time;
    	}
    }

    //startTime='12:08' endTime=15:24
    public static double getDiffHours(String endTime, String startTime) {
    	//todo
    	if (CommonUtil.isEmptyText(endTime)) {
    		endTime = "18:00";
    	}
    	if (CommonUtil.isEmptyText(startTime)) {
    		startTime = "09:00";
    	}
    	//
    	
    	int startHour = Integer.parseInt(startTime.substring(0, 2));
    	int startMinute = Integer.parseInt(startTime.substring(3, 5));
    	
    	int endHour = Integer.parseInt(endTime.substring(0, 2));
    	int endMinute = Integer.parseInt(endTime.substring(3, 5));
    	
    	return ((endHour*60+endMinute)-(startHour*60+startMinute))/60.0;
    }
}
 